#include "s_xpt2046_user.h"
#include "s_xpt2046.h"


#include <stdio.h>
#include <string.h>

static s_xpt2046_t xpt2046_t;
//////////////////////////////////////////////////////////////////////////////////
#include "h_stm32_hal_defines.h"
#include "h_stm32_hal_spi.h"
#include "h_stm32_hal_timer.h"

#include "h_spi_s_user.h"

//////////////////////////////////////////////////////////////////////////////////
signed char s_xpt2046_user_init(void)
{
    unsigned char init_data = 0x80;

    // xpt2046_t.cs_enable = &h_stm32_hal_spi2_cs_enable;
    // xpt2046_t.cs_disable = &h_stm32_hal_spi2_cs_disable;
    // xpt2046_t.write = &h_stm32_hal_spi2_write;
    // xpt2046_t.read = &h_stm32_hal_spi2_read;
    xpt2046_t.cs_enable = &h_spi_s_user_cs_enable;
    xpt2046_t.cs_disable = &h_spi_s_user_cs_dsiable;
    xpt2046_t.write = &h_spi_s_user_write_bytes;
    xpt2046_t.read = &h_spi_s_user_read_bytes;
    xpt2046_t.delay_us = &delay_us;

    xpt2046_t.cs_enable();
    xpt2046_t.write(&init_data, 1);
    xpt2046_t.cs_disable();
    return 0;
}

signed char s_xpt2046_user_get_position(void)
{
    // s_xpt2046_state rslt = s_xpt2046_read_raw_pos(&xpt2046_t);
    // // printf("r:%d\r\n", rslt);
    // return rslt;
    return s_xpt2046_read_raw_pos(&xpt2046_t);
}

signed short s_xpt2046_user_get_position_x(void)
{
    return xpt2046_t.raw_x;
}

signed short s_xpt2046_user_get_position_y(void)
{
    return xpt2046_t.raw_y;
}

signed char s_xpt2046_user_test(void)
{
    if(s_xpt2046_ok == s_xpt2046_read_raw_pos(&xpt2046_t))
    {
        printf("%d, %d\r\n", xpt2046_t.raw_x, xpt2046_t.raw_y);
    }
    else
    {
        printf("read raw failed\r\n");
    }
    return 0;
}